    //
//  SettingMainViewController.m
//  PhotoFrame
//
//  Created by William on 11-5-6.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "SettingMainViewController.h"
#import "SettingPhotoViewController.h"
#import "SettingVideoViewController.h"
#import "SettingLayoutViewController.h"
#import "SettingPollingViewController.h"
#import "GlobalSettingUtils.h"

@implementation SettingMainViewController
@synthesize listData;
@synthesize controllers;
@synthesize allControllers;
@synthesize names;
@synthesize keys;
@synthesize myTableView;
@synthesize delegate;
/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
	UIColor *color = [UIColor colorWithRed:247.0/255.0 green:247.0/255.0 blue:247.0/255.0 alpha:1.0];
	[self.myTableView setBackgroundColor:color];
	[self.view addSubview:self.myTableView];
	
	UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] 
								   initWithTitle:@"Done" 
								   style:UIBarButtonItemStyleBordered 
								   target:self action:@selector(closePop)];
	self.navigationItem.rightBarButtonItem = doneButton;
	[doneButton release];
	
	NSString *path = [[NSBundle mainBundle] pathForResource:@"mainsection" ofType:@"plist"];
	NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];
	
	
	self.names = dict;
	[dict release];
	
	path = [[NSBundle mainBundle] pathForResource:@"mainsectionorder" ofType:@"plist"];
	NSArray *array = [[NSArray alloc] initWithContentsOfFile:path];
	//NSArray *array = [names allKeys];

	//[array sortedArrayUsingSelector:@selector(compare:)];
	
	self.keys = array;
	//[array release];
	//array = nil;
	
	/*
	array = [[NSMutableArray alloc] initWithObjects:@"フレーム", @"トランジション", nil];
	self.listData = array;
	[array release];
	array = nil;
	 */
	NSMutableDictionary *mutableDict = [[NSMutableDictionary alloc] init];
	
	int index = 0;
	
	NSMutableArray *mutableArray = [[NSMutableArray alloc] init];
	SettingPhotoViewController *photoViewController = [[SettingPhotoViewController alloc] initWithStyle:UITableViewStyleGrouped];
	photoViewController.delegate = self.delegate;
	[mutableArray addObject:photoViewController];
	[photoViewController release];
	
	SettingVideoViewController *videoViewController = [[SettingVideoViewController alloc] initWithStyle:UITableViewStyleGrouped];
	videoViewController.delegate = self.delegate;
	[mutableArray addObject:videoViewController];
	[videoViewController release];
	
	[mutableDict setObject:mutableArray forKey:[self.keys objectAtIndex:(NSUInteger) index++]];
	
	[mutableArray release];
	mutableArray = nil;
	
	mutableArray = [[NSMutableArray alloc] init];
	SettingLayoutViewController *layoutViewController = [[SettingLayoutViewController alloc] initWithStyle:UITableViewStyleGrouped];
	layoutViewController.delegate = self.delegate;
	[mutableArray addObject:layoutViewController];
	[layoutViewController release];
	
	SettingPollingViewController *pollingViewController = [[SettingPollingViewController alloc] 
														   initWithNibName:@"SettingPollingView" bundle:nil];
	pollingViewController.delegate = self.delegate;
	[mutableArray addObject:pollingViewController];
	[pollingViewController release];
	
	self.controllers = mutableArray;
	
	[mutableDict setObject:mutableArray forKey:[self.keys objectAtIndex:(NSUInteger) index]];
	self.allControllers = mutableDict;
	[mutableArray release];
	[mutableDict release];
    [super viewDidLoad];
}

- (void) closePop {
	[delegate closePopover];
}

- (void) viewWillAppear:(BOOL)animated {
	[super viewWillAppear:animated];
	[self.myTableView reloadData];
}

- (void) viewDidAppear:(BOOL)animated {
	[super viewWillAppear:animated];
	[self.tableView reloadData];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Overriden to allow any orientation.
    return YES;
}


- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark -
#pragma mark Table View Data Source Methods
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
	return [keys count];
}


- (NSInteger) tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section {
	NSString *key = [keys objectAtIndex:section];
	NSArray *nameSection = [names objectForKey:key];
	return [nameSection count];
}

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	NSUInteger section = [indexPath section];
	NSUInteger row = [indexPath row];
	
	NSString *key = [keys objectAtIndex:section];
	NSArray *nameSection = [names objectForKey:key];
	
	static NSString *identify = @"SimpleTableIdentifier";
	
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identify];
	if (cell == nil) {
		cell = [[[UITableViewCell alloc] 
				 initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identify] autorelease];
	}
	
	NSString *text = [nameSection objectAtIndex:row];
	cell.textLabel.text = text;
	NSArray *array = [self.allControllers objectForKey:key];
	if (section == 0 && array != nil && [array count] > 0) {
		cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
		if (row == 0) {
			[cell.imageView setImage:[UIImage imageNamed:@"image.png"]];
		} else if (row == 1) {
			[cell.imageView setImage:[UIImage imageNamed:@"video.png"]];
		}
	} else if (section == 1 && array != nil && [array count] > 0) {
		cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
		UILabel *contentView = [[UILabel alloc] initWithFrame:CGRectMake(170, 0, 120, 43)];
		[contentView setFont:[UIFont systemFontOfSize: 17.0f]];
		contentView.textColor = [UIColor colorWithRed:87.0/255.0 green:158.0/255.0 blue:212.0/255.0 alpha:1];
        contentView.backgroundColor = [UIColor colorWithRed:247.0/255.0 green:247.0/255.0 blue:247.0/255.0 alpha:1];
        
		[contentView setTextAlignment:UITextAlignmentRight];
		if ([text isEqualToString:@"フレーム"]) {
			NSString *layout = [[GlobalSettingUtils getGlobalConf] objectForKey:@"layout"];
			NSString *lblText = @"";
			switch ([layout intValue]) {
				case 1:
					lblText = @"下フレーム";
					break;
				case 2:
					lblText = @"右フレーム";
					break;
				case 3:
					lblText = @"フレームなし";
					break;
                case 4:
					lblText = @"グミフレーム";
					break;
			}
			[contentView setText:lblText];
		} else {
			NSString *polling = [[GlobalSettingUtils getGlobalConf] objectForKey:@"polling"];
			[contentView setText:[NSString stringWithFormat:@"%d%@", [polling intValue], @"秒"]];
		}
		[cell.contentView addSubview:contentView];
		[contentView release];
	} else {
		cell.textLabel.textAlignment = UITextAlignmentCenter;
	}


	return cell;
}

- (NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
	NSString *key = [[keys objectAtIndex:section] substringFromIndex:(NSUInteger)2];
	return key;
}



#pragma mark -
#pragma mark Table View Delegate Methods
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	NSUInteger section = [indexPath section];
	NSUInteger row = [indexPath row];
	
	NSString *key = [self.keys objectAtIndex:section];
	
	NSArray *array = [self.allControllers objectForKey:key];
	if (array != nil && [array count] > 0) {
		SettingLayoutViewController *nextController = [array objectAtIndex:row];
		[self.navigationController pushViewController:nextController animated:YES];
	}
	
	if (section == 2) {
		NSString *iTunesLink;
		iTunesLink = @"http://www.ucdesign.co.jp/dpf";
		[[UIApplication sharedApplication] openURL:[NSURL URLWithString:iTunesLink]];
	}
	
	[tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
	UIViewController *footerViewController = [[[UIViewController alloc] initWithNibName:@"BrandView" bundle:nil] autorelease];
	if (section == [self.keys count] - 1) {
		return footerViewController.view ;
	}
	return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
	if (section == [self.keys count] - 1) {
		return 240.0f;
	}
	return 0.0f;
}


- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
	self.listData = nil;
	self.controllers = nil;
	self.allControllers = nil;
	self.myTableView = nil;
}

- (void)dealloc {
	[listData release];
	[controllers release];
	[allControllers release];
	[myTableView release];
    [super dealloc];
}
@end
